package tat.domain;

import java.util.Observable;

import tat.mock.DomainModelRelationFactoryImpl;

public abstract class DomainModelRelation extends Relation {

	private String type;
	
    private DomainModelRelationEndPoint origin;

    private DomainModelRelationEndPoint destination;

    private boolean navigable;

    public DomainModelRelation(String relationType, final DomainModelRelationEndPoint origin, final DomainModelRelationEndPoint destination, final boolean navigable) {
        this.origin = origin;
        this.destination = destination;
        this.navigable = navigable;
        this.type = relationType;
    }

    public DomainModelRelationEndPoint origin() {
        return origin;
    }

    public DomainModelRelationEndPoint destination() {
        return destination;
    }

    public boolean isNavigable() {
        return navigable;
    }
    
    public String name() {
    	String name = null;
    	if (DomainModelRelationFactoryImpl.ASSOCIATION.equals(type)) {
    		name = "'" + origin.tatClass().name() + " USES " + destination.tatClass().name() + "'";
    	}
    	if (DomainModelRelationFactoryImpl.AGREGATION.equals(type)) {
    		name = "'" + origin.tatClass().name() + " AGGREGATES " + destination.tatClass().name() + "'";
    	}
    	if (DomainModelRelationFactoryImpl.COMPOSITION.equals(type)) {
    		name = "'" + origin.tatClass().name() + " CONSISTS OF " + destination.tatClass().name() + "'";
    	}
    	return name;
    }
    
    public String requirementValue(){
    	String name = null;
    	if (DomainModelRelationFactoryImpl.ASSOCIATION.equals(type)) {
    		name = "'" + origin.multiplicity() + " " + origin.tatClass().name() + " USES " + destination.multiplicity() + " " + destination.tatClass().name() + "'";
    	}
    	if (DomainModelRelationFactoryImpl.AGREGATION.equals(type)) {
    		name = "'" + origin.multiplicity() + " " + origin.tatClass().name() + " AGGREGATES " + destination.multiplicity() + " " + destination.tatClass().name() + "'";
    	}
    	if (DomainModelRelationFactoryImpl.COMPOSITION.equals(type)) {
    		name = "'" + origin.multiplicity() + " " + origin.tatClass().name() + " CONSISTS OF " + destination.multiplicity() + " " + destination.tatClass().name() + "'";
    	}
    	return name;
    }
    
    public String toString() {
    	return "\t" + this.name() + "\n";
	}

	public String toXML() {
		return "\t<domain-model-relation>" + this.name() + "</domain-model-relation>\n";
	}
    
    public String relationType(){
    	return type;
    }

    /**
     * TODO Método mock.
     */
    @Override
    public Requirement requirement() {
    	//TODO complementar
        return new RelationRequirement("");
    }
    
    
	@Override
	public void update(Observable o, Object arg) {
		setChanged();
		notifyObservers();
	}

}
